﻿using System;
using System.Collections.Generic;
using UnityEngine;
using BridgeShips.AI;

namespace BridgeShips.Building
{
	public enum BuildingSpace { None = 0, Auxiliary = 1, Setting = 2, Reset = 5, Exit = 10, Animation01 = 15, Animation02 = 20, Help = 25, Other = 30, Window = 35 }

	public enum PieceState { Preview, Placed }


	public class BuildingPiece : MonoBehaviour
    {
		public PieceState State { get { return m_State; } }

		public BuildingHolder Building { get; set; }

		public BuildingPiece AttachedOn { get; set; }

        public string Name { get { return m_PieceName; } }

		public RequiredItem[] RequiredItems { get { return m_RequiredItems; } }

		public Vector3 RotationAxis { get { return m_RotationAxis; } }

		public BuildingSpace NeededSpace { get { return m_NeededSpace; } }

		public BuildingSpace[] SpacesToOccupy { get { return m_SpacesToOccupy; } }

        public float OutOfGroundHeight { get { return m_OutOfGroundHeight; } }
        
        public bool AllowUnderTerrainMovement { get { return m_AllowUnderTerrainMovement; } }

        public bool RequiresSockets { get { return m_RequiresSockets; } }

        public SoundPlayer BuildAudio { get { return m_BuildAudio; } }

        public GameObject PlacementFX { get { return m_PlacementFX; } }

        public void SetTrigger(string name ) {

        }


        public Bounds Bounds { get { return new Bounds(transform.position + transform.TransformVector(m_Bounds.center), m_Bounds.size); } }

        [SerializeField]
        private string m_PieceName = null;

		[SerializeField]
		private RequiredItem[] m_RequiredItems;

		[SerializeField]
		private Vector3 m_RotationAxis = Vector3.forward;

		[Header("Setup")]

		[SerializeField]
		private bool m_ShowBounds;

		[SerializeField]
		private Bounds m_Bounds;


		[SerializeField]
		private List<Collider> m_IgnoredColliders;

		[Header("Placing")]

		[SerializeField]
		private BuildingSpace m_NeededSpace ;

        [SerializeField]
		private BuildingSpace[] m_SpacesToOccupy ;

        [SerializeField]
        private bool m_RequiresSockets ;

        [SerializeField]
        private float m_OutOfGroundHeight ;

        [SerializeField]
        private bool m_AllowUnderTerrainMovement ;

        [Header("Stability")]

        [SerializeField]
        private bool m_CheckStability = true;

		[Space]

        [SerializeField]
        private LayerMask m_StabilityCheckMask ;

        [SerializeField]
		private bool m_ShowStabilityBox ;

        [SerializeField]
		private Bounds m_StabilityBox;

        [Header( "Terrain Protection" )]

   

		[SerializeField]
		private bool m_ShowTP = false;

        [SerializeField]
		private Bounds m_TPBox;

        [Header( "Sound And Effects" )]

        [SerializeField]
        private SoundPlayer m_BuildAudio = null;

        [SerializeField]
        private GameObject m_PlacementFX = null;


		private PieceState m_State = PieceState.Preview;


		private bool m_Initialized;


        private void Awake()
        {

			m_Initialized = true;
        }

        public void SetState(PieceState state)
		{
			if(!m_Initialized)
				Awake();

			if(state == PieceState.Preview)
			{
                
			}
			else if(state == PieceState.Placed)
			{
				
				if(m_CheckStability && AttachedOn != null)
				{
					var eventHandler = AttachedOn.GetComponent<EntityEventHandler>();
                    
				}
            }

			m_State = state;
		}

		private void On_SocketDeath()
		{
			if(gameObject != null)
			{
				EntityEventHandler entityHandler = GetComponent<EntityEventHandler>();
				
			}
		}

		private void OnDestroy()
		{

		}

        private void OnDrawGizmosSelected()
        {
			var oldMatrix = Gizmos.matrix;
			Gizmos.color = Color.blue;

			// Draw the bounds
			if(m_ShowBounds)
			{
				Gizmos.matrix = Matrix4x4.TRS(transform.position + transform.TransformVector(m_Bounds.center), transform.rotation, m_Bounds.size);
				Gizmos.DrawWireCube(Vector3.zero, Vector3.one);
			}

			// Draw the terrain protection box
			Gizmos.color = Color.yellow;
			if(m_ShowTP)
			{
				Gizmos.matrix = Matrix4x4.TRS(transform.position + transform.TransformVector(m_TPBox.center), transform.rotation, m_TPBox.size);
				Gizmos.DrawWireCube(Vector3.zero, Vector3.one);
			}

			// Draw the stability box
			Gizmos.color = Color.red;
			if(m_ShowStabilityBox)
			{
				Gizmos.matrix = Matrix4x4.TRS(transform.position + transform.TransformVector(m_StabilityBox.center), transform.rotation, m_StabilityBox.size);
				Gizmos.DrawWireCube(Vector3.zero, Vector3.one);
			}

			Gizmos.matrix = oldMatrix;
        }
    }
}